package com.example.sort.resultShow;

import com.alibaba.fastjson.JSONObject;
import com.example.sort.constant.SortConstant;
import com.example.sort.entity.RectangleInfo;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.List;
import java.util.Random;

/**
 * @author Archer
 */
public class ResultShow extends Application {
    public static String allNodes="[{\"nodeX\":0.0,\"nodeY\":0.0,\"length\":129.0,\"width\":116.0,\"nodeEndX\":129.0,\"nodeEndY\":116.0,\"isOrder\":true,\"area\":14964.0},{\"nodeX\":0.0,\"nodeY\":116.0,\"length\":205.0,\"width\":95.0,\"nodeEndX\":205.0,\"nodeEndY\":211.0,\"isOrder\":true,\"area\":19475.0},{\"nodeX\":0.0,\"nodeY\":211.0,\"length\":135.0,\"width\":100.0,\"nodeEndX\":135.0,\"nodeEndY\":311.0,\"isOrder\":true,\"area\":13500.0},{\"nodeX\":0.0,\"nodeY\":311.0,\"length\":58.0,\"width\":77.0,\"nodeEndX\":58.0,\"nodeEndY\":388.0,\"isOrder\":true,\"area\":4466.0},{\"nodeX\":129.0,\"nodeY\":0.0,\"length\":150.0,\"width\":96.0,\"nodeEndX\":279.0,\"nodeEndY\":96.0,\"isOrder\":true,\"area\":14400.0},{\"nodeX\":58.0,\"nodeY\":311.0,\"length\":154.0,\"width\":51.0,\"nodeEndX\":212.0,\"nodeEndY\":362.0,\"isOrder\":true,\"area\":7854.0},{\"nodeX\":135.0,\"nodeY\":211.0,\"length\":193.0,\"width\":89.0,\"nodeEndX\":328.0,\"nodeEndY\":300.0,\"isOrder\":true,\"area\":17177.0},{\"nodeX\":205.0,\"nodeY\":96.0,\"length\":121.0,\"width\":70.0,\"nodeEndX\":326.0,\"nodeEndY\":166.0,\"isOrder\":true,\"area\":8470.0},{\"nodeX\":326.0,\"nodeY\":0.0,\"length\":167.0,\"width\":107.0,\"nodeEndX\":493.0,\"nodeEndY\":107.0,\"isOrder\":true,\"area\":17869.0},{\"nodeX\":58.0,\"nodeY\":362.0,\"length\":157.0,\"width\":20.0,\"nodeEndX\":215.0,\"nodeEndY\":382.0,\"isOrder\":true,\"area\":3140.0},{\"nodeX\":326.0,\"nodeY\":107.0,\"length\":169.0,\"width\":102.0,\"nodeEndX\":495.0,\"nodeEndY\":209.0,\"isOrder\":true,\"area\":17238.0},{\"nodeX\":215.0,\"nodeY\":300.0,\"length\":119.0,\"width\":89.0,\"nodeEndX\":334.0,\"nodeEndY\":389.0,\"isOrder\":true,\"area\":10591.0},{\"nodeX\":328.0,\"nodeY\":209.0,\"length\":71.0,\"width\":84.0,\"nodeEndX\":399.0,\"nodeEndY\":293.0,\"isOrder\":true,\"area\":5964.0},{\"nodeX\":334.0,\"nodeY\":293.0,\"length\":201.0,\"width\":76.0,\"nodeEndX\":535.0,\"nodeEndY\":369.0,\"isOrder\":true,\"area\":15276.0},{\"nodeX\":493.0,\"nodeY\":0.0,\"length\":140.0,\"width\":87.0,\"nodeEndX\":633.0,\"nodeEndY\":87.0,\"isOrder\":true,\"area\":12180.0},{\"nodeX\":399.0,\"nodeY\":209.0,\"length\":132.0,\"width\":81.0,\"nodeEndX\":531.0,\"nodeEndY\":290.0,\"isOrder\":true,\"area\":10692.0},{\"nodeX\":495.0,\"nodeY\":87.0,\"length\":143.0,\"width\":70.0,\"nodeEndX\":638.0,\"nodeEndY\":157.0,\"isOrder\":true,\"area\":10010.0},{\"nodeX\":495.0,\"nodeY\":157.0,\"length\":147.0,\"width\":33.0,\"nodeEndX\":642.0,\"nodeEndY\":190.0,\"isOrder\":true,\"area\":4851.0},{\"nodeX\":531.0,\"nodeY\":190.0,\"length\":106.0,\"width\":100.0,\"nodeEndX\":637.0,\"nodeEndY\":290.0,\"isOrder\":true,\"area\":10600.0},{\"nodeX\":279.0,\"nodeY\":0.0,\"length\":41.0,\"width\":89.0,\"nodeEndX\":320.0,\"nodeEndY\":89.0,\"isOrder\":true,\"area\":3649.0},{\"nodeX\":535.0,\"nodeY\":290.0,\"length\":203.0,\"width\":79.0,\"nodeEndX\":738.0,\"nodeEndY\":369.0,\"isOrder\":true,\"area\":16037.0},{\"nodeX\":205.0,\"nodeY\":166.0,\"length\":118.0,\"width\":44.0,\"nodeEndX\":323.0,\"nodeEndY\":210.0,\"isOrder\":true,\"area\":5192.0},{\"nodeX\":637.0,\"nodeY\":190.0,\"length\":163.0,\"width\":99.0,\"nodeEndX\":800.0,\"nodeEndY\":289.0,\"isOrder\":true,\"area\":16137.0},{\"nodeX\":null,\"nodeY\":null,\"length\":216.0,\"width\":104.0,\"nodeEndX\":null,\"nodeEndY\":null,\"isOrder\":false,\"area\":22464.0},{\"nodeX\":334.0,\"nodeY\":369.0,\"length\":104.0,\"width\":22.0,\"nodeEndX\":438.0,\"nodeEndY\":391.0,\"isOrder\":true,\"area\":2288.0},{\"nodeX\":638.0,\"nodeY\":0.0,\"length\":154.0,\"width\":103.0,\"nodeEndX\":792.0,\"nodeEndY\":103.0,\"isOrder\":true,\"area\":15862.0},{\"nodeX\":642.0,\"nodeY\":103.0,\"length\":99.0,\"width\":55.0,\"nodeEndX\":741.0,\"nodeEndY\":158.0,\"isOrder\":true,\"area\":5445.0},{\"nodeX\":438.0,\"nodeY\":369.0,\"length\":197.0,\"width\":24.0,\"nodeEndX\":635.0,\"nodeEndY\":393.0,\"isOrder\":true,\"area\":4728.0},{\"nodeX\":738.0,\"nodeY\":289.0,\"length\":24.0,\"width\":100.0,\"nodeEndX\":762.0,\"nodeEndY\":389.0,\"isOrder\":true,\"area\":2400.0},{\"nodeX\":null,\"nodeY\":null,\"length\":80.0,\"width\":104.0,\"nodeEndX\":null,\"nodeEndY\":null,\"isOrder\":false,\"area\":8320.0},{\"nodeX\":762.0,\"nodeY\":289.0,\"length\":36.0,\"width\":100.0,\"nodeEndX\":798.0,\"nodeEndY\":389.0,\"isOrder\":true,\"area\":3600.0},{\"nodeX\":null,\"nodeY\":null,\"length\":184.0,\"width\":102.0,\"nodeEndX\":null,\"nodeEndY\":null,\"isOrder\":false,\"area\":18768.0},{\"nodeX\":null,\"nodeY\":null,\"length\":120.0,\"width\":79.0,\"nodeEndX\":null,\"nodeEndY\":null,\"isOrder\":false,\"area\":9480.0},{\"nodeX\":null,\"nodeY\":null,\"length\":162.0,\"width\":64.0,\"nodeEndX\":null,\"nodeEndY\":null,\"isOrder\":false,\"area\":10368.0},{\"nodeX\":741.0,\"nodeY\":103.0,\"length\":55.0,\"width\":80.0,\"nodeEndX\":796.0,\"nodeEndY\":183.0,\"isOrder\":true,\"area\":4400.0},{\"nodeX\":null,\"nodeY\":null,\"length\":41.0,\"width\":49.0,\"nodeEndX\":null,\"nodeEndY\":null,\"isOrder\":false,\"area\":2009.0},{\"nodeX\":null,\"nodeY\":null,\"length\":121.0,\"width\":78.0,\"nodeEndX\":null,\"nodeEndY\":null,\"isOrder\":false,\"area\":9438.0},{\"nodeX\":null,\"nodeY\":null,\"length\":94.0,\"width\":96.0,\"nodeEndX\":null,\"nodeEndY\":null,\"isOrder\":false,\"area\":9024.0},{\"nodeX\":null,\"nodeY\":null,\"length\":68.0,\"width\":60.0,\"nodeEndX\":null,\"nodeEndY\":null,\"isOrder\":false,\"area\":4080.0},{\"nodeX\":null,\"nodeY\":null,\"length\":134.0,\"width\":77.0,\"nodeEndX\":null,\"nodeEndY\":null,\"isOrder\":false,\"area\":10318.0},{\"nodeX\":null,\"nodeY\":null,\"length\":136.0,\"width\":94.0,\"nodeEndX\":null,\"nodeEndY\":null,\"isOrder\":false,\"area\":12784.0},{\"nodeX\":null,\"nodeY\":null,\"length\":198.0,\"width\":54.0,\"nodeEndX\":null,\"nodeEndY\":null,\"isOrder\":false,\"area\":10692.0},{\"nodeX\":null,\"nodeY\":null,\"length\":61.0,\"width\":89.0,\"nodeEndX\":null,\"nodeEndY\":null,\"isOrder\":false,\"area\":5429.0},{\"nodeX\":635.0,\"nodeY\":369.0,\"length\":101.0,\"width\":26.0,\"nodeEndX\":736.0,\"nodeEndY\":395.0,\"isOrder\":true,\"area\":2626.0},{\"nodeX\":null,\"nodeY\":null,\"length\":45.0,\"width\":37.0,\"nodeEndX\":null,\"nodeEndY\":null,\"isOrder\":false,\"area\":1665.0},{\"nodeX\":null,\"nodeY\":null,\"length\":184.0,\"width\":25.0,\"nodeEndX\":null,\"nodeEndY\":null,\"isOrder\":false,\"area\":4600.0},{\"nodeX\":null,\"nodeY\":null,\"length\":91.0,\"width\":72.0,\"nodeEndX\":null,\"nodeEndY\":null,\"isOrder\":false,\"area\":6552.0},{\"nodeX\":null,\"nodeY\":null,\"length\":78.0,\"width\":61.0,\"nodeEndX\":null,\"nodeEndY\":null,\"isOrder\":false,\"area\":4758.0},{\"nodeX\":642.0,\"nodeY\":158.0,\"length\":54.0,\"width\":31.0,\"nodeEndX\":696.0,\"nodeEndY\":189.0,\"isOrder\":true,\"area\":1674.0},{\"nodeX\":null,\"nodeY\":null,\"length\":201.0,\"width\":94.0,\"nodeEndX\":null,\"nodeEndY\":null,\"isOrder\":false,\"area\":18894.0},{\"nodeX\":null,\"nodeY\":null,\"length\":68.0,\"width\":67.0,\"nodeEndX\":null,\"nodeEndY\":null,\"isOrder\":false,\"area\":4556.0},{\"nodeX\":null,\"nodeY\":null,\"length\":114.0,\"width\":67.0,\"nodeEndX\":null,\"nodeEndY\":null,\"isOrder\":false,\"area\":7638.0},{\"nodeX\":null,\"nodeY\":null,\"length\":80.0,\"width\":47.0,\"nodeEndX\":null,\"nodeEndY\":null,\"isOrder\":false,\"area\":3760.0},{\"nodeX\":null,\"nodeY\":null,\"length\":93.0,\"width\":78.0,\"nodeEndX\":null,\"nodeEndY\":null,\"isOrder\":false,\"area\":7254.0},{\"nodeX\":null,\"nodeY\":null,\"length\":127.0,\"width\":83.0,\"nodeEndX\":null,\"nodeEndY\":null,\"isOrder\":false,\"area\":10541.0},{\"nodeX\":null,\"nodeY\":null,\"length\":211.0,\"width\":25.0,\"nodeEndX\":null,\"nodeEndY\":null,\"isOrder\":false,\"area\":5275.0},{\"nodeX\":null,\"nodeY\":null,\"length\":110.0,\"width\":44.0,\"nodeEndX\":null,\"nodeEndY\":null,\"isOrder\":false,\"area\":4840.0},{\"nodeX\":null,\"nodeY\":null,\"length\":145.0,\"width\":64.0,\"nodeEndX\":null,\"nodeEndY\":null,\"isOrder\":false,\"area\":9280.0},{\"nodeX\":null,\"nodeY\":null,\"length\":93.0,\"width\":75.0,\"nodeEndX\":null,\"nodeEndY\":null,\"isOrder\":false,\"area\":6975.0},{\"nodeX\":null,\"nodeY\":null,\"length\":195.0,\"width\":110.0,\"nodeEndX\":null,\"nodeEndY\":null,\"isOrder\":false,\"area\":21450.0}]";
//    public static String allNodes="";
//    public static String allNodes="";
//    public static String allNodes="";

    public static void main(String[] args) throws IOException {
        launch(args);
    }

    public String createColor() {
        //红色
        String red;
        //绿色
        String green;
        //蓝色
        String blue;
        //生成随机对象
        Random random = new Random();
        //生成红色颜色代码
        red = Integer.toHexString(random.nextInt(256)).toUpperCase();
        //生成绿色颜色代码
        green = Integer.toHexString(random.nextInt(256)).toUpperCase();
        //生成蓝色颜色代码
        blue = Integer.toHexString(random.nextInt(256)).toUpperCase();

        //判断红色代码的位数
        red = red.length() == 1 ? "0" + red : red;
        //判断绿色代码的位数
        green = green.length() == 1 ? "0" + green : green;
        //判断蓝色代码的位数
        blue = blue.length() == 1 ? "0" + blue : blue;
        //生成十六进制颜色值
        String color = "#" + red + green + blue;
        return color;
    }

    @Override
    public void start(Stage stage) throws Exception {
        //将得到的结果复制进入allNodes，就可以将图形展示出来
        List<RectangleInfo> points = JSONObject.parseArray(allNodes, RectangleInfo.class);
        if(points.size()==0)
        {
            RectangleInfo test=new RectangleInfo();
            test.setNodeX(0f);
            test.setNodeY(0f);
            test.setLength(200f);
            test.setWidth(100f);
            test.setIsOrder(true);
            points.add(test);
        }

        //根据大矩形的宽高设置画布大小
        Canvas canvas = new Canvas(SortConstant.length * 2, SortConstant.width * 2);
        GraphicsContext gc = canvas.getGraphicsContext2D();
        Group root = new Group();
        root.getChildren().add(canvas);

        Scene scene = new Scene(root);
        stage.setScene(scene);
        for (int i = 0; i < points.size(); i++) {
            if(points.get(i).getIsOrder()){
                double x = points.get(i).getNodeX();
                double y = points.get(i).getNodeY();
                double width = points.get(i).getLength();
                double height = points.get(i).getWidth();
                //在画布上画矩形
                gc.setFill(Paint.valueOf(createColor()));
                gc.fillRect(x, y, width, height);
                gc.setFont(Font.font(10));
                gc.setFill(Paint.valueOf("black"));
                gc.fillText(width + "x\n" + height, x, y + 10);
            }
        }

        stage.setTitle("2D排样显示图 ");
        stage.setWidth(SortConstant.length +20);
        stage.setHeight(SortConstant.width + 50);
        System.out.println(getMsg(points));
        stage.show();
//        stage.setMaxWidth(init.getWidth());
//        stage.setMaxHeight(init.getHeight());

    }

    /**
     * 获取返回结果
     * @param list
     * @return
     */
    private String getMsg(List<RectangleInfo> list){
        Double areaUsing = list.stream().filter(x -> x.getIsOrder().equals(true)).mapToDouble(x->(x.getWidth()*x.getLength())).sum();
        Long count=list.stream().filter(x->x.getIsOrder()==true).count();
        Integer allCount=list.size();
        return "面积利用率达到"+(areaUsing*100/(SortConstant.width*SortConstant.length))+"%,总共"+allCount+"个矩形，排序了"+count+"个。";
    }
}
